package com.travel.server.manager.impl.command.project;

import com.travel.common.util.CollectionUtil;
import com.travel.server.manager.dto.request.project.UpdateProjectDetailInfoRequest;
import com.travel.server.manager.dto.request.project.UpdateProjectDetailInfoRequest.ArrangeItem;
import com.travel.server.manager.dto.response.project.UpdateProjectDetailInfoResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.ProjectDal;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.impl.youzan.YouzanSal;
import com.travel.server.manager.model.ProjectInfo;
import com.travel.server.manager.model.ProjectSpecial;
import com.travel.server.manager.model.UserInfo;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemUpdateParams;
import com.youzan.open.sdk.gen.v3_0_0.model.YouzanItemsCustomGetResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/10/31 18:29
 * @Desc:
 */
@Component
@Slf4j
public class UpdateProjectDetailInfoCommand extends AbstractCommand<UpdateProjectDetailInfoRequest, UpdateProjectDetailInfoResponse> {

    @Autowired
    private ProjectDal projectDal;

    @Autowired
    private SpecialDal specialDal;

    @Override
    protected void checkInput(UpdateProjectDetailInfoRequest request) {

    }

    @Override
    public UpdateProjectDetailInfoResponse handle(UpdateProjectDetailInfoRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        ProjectInfo projectInfo = projectDal.getProjectInfo(request.getId(), userInfo);

        projectInfo.setBackground(request.getBackground());

        YouzanItemUpdateParams param = new YouzanItemUpdateParams();
        YouzanItemsCustomGetResult youzanItemsCustomGetResult = YouzanSal.queryItem(projectInfo.getNumber());
        if(youzanItemsCustomGetResult != null && youzanItemsCustomGetResult.getItems() != null && youzanItemsCustomGetResult.getItems().length > 0) {
            Long itemId = youzanItemsCustomGetResult.getItems()[0].getItemId();
            param.setItemId(itemId);
            param.setDesc(request.getBackground());
            YouzanSal.updateYouzanItem(param);
        }

        projectInfo.setUpdateBy(userInfo.getName());
        projectInfo.setUpdateTime(now);

        projectDal.insertOrUpdate(projectInfo);

        List<ArrangeItem> arrangeList = request.getArrangeList();
        if(!CollectionUtil.isEmpty(arrangeList)){
            updateArrange(arrangeList, userInfo);
        }

        UpdateProjectDetailInfoResponse response = new UpdateProjectDetailInfoResponse();
        response.setId(projectInfo.getId());

        return response;
    }

    public void updateArrange(List<ArrangeItem> arrangeItemList, UserInfo userInfo){
        for(ArrangeItem arrangeItem : arrangeItemList){
            int id = arrangeItem.getArrangeId();
            ProjectSpecial projectSpecial = specialDal.selectById(id);
            Integer projectId = projectSpecial.getProjectId();
            specialDal.updateByDays(arrangeItem.getExplains(), projectSpecial.getDays(), projectId, userInfo.getName());
        }
    }
}
